package api

import (
	"api-gateway/basic/config"
	"api-gateway/basic/inits"
	h "api-gateway/basic/proto"
	__ "api-gateway/basic/user_proto"
	"api-gateway/handler/request"
	"api-gateway/handler/response"
	"api-gateway/midderware"
	"context"
	"fmt"
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	"time"
)

// SendSms
// @Summary      短信发送
// @Description  发送一个新的短信
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.SendSms  true  "短信参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /user/sendSms [post]
func SendSms(c *gin.Context) {
	var v request.SendSms
	if err := c.ShouldBind(&v); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	sms, err := inits.UserClient.SendSms(c, &__.SendSmsReq{Mobile: v.Mobile})
	if err != nil {
		response.Error(c, 500, "短信发送失败", err.Error())
		return
	}
	response.Error(c, 200, "短信发送成功", sms)

}

// CreateUser
// @Summary		 注册用户
// @Description  注册一个姓用户
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.Register  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /user/createUser [post]
func CreateUser(c *gin.Context) {
	var v request.Register
	if err := c.ShouldBind(&v); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	_, err := inits.UserClient.CreateUser(c, &__.CreateUserReq{
		Mobile:   v.Mobile,
		Password: v.Password,
		Nickname: v.Nickname,
	})
	if err != nil {
		response.Error(c, 500, "注册失败", err.Error())
		return
	}
	response.Success(c, "注册成功", nil)
}

// Login
// @Summary      用户登录
// @Description  登录一个新用户
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.Login  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Router       /user/login [post]
func Login(c *gin.Context) {
	var v request.Login
	if err := c.ShouldBind(&v); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	if inits.UserClient == nil {
		// 处理空指针情况（如返回错误、初始化默认值等）
		response.Error(c, 500, "UserClient 未初始化", nil)
		return
	}

	u, err := inits.UserClient.Login(c, &__.LoginReq{
		Mobile:   v.Mobile,
		Password: v.Password,
	})
	if err != nil {
		response.Error(c, 500, "登录失败", err.Error())
		return
	}
	fmt.Println(u)
	token, err := midderware.NewJWT(config.KEY_JWT).CreateToken(midderware.CustomClaims{
		ID:          uint(u.Id),
		NickName:    u.Nickname,
		AuthorityId: uint(u.Status),
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: time.Now().Unix() + 3600,
		},
	})
	if err != nil {
		response.Error(c, 500, "token生成失败", err.Error())
		return
	}
	response.Success(c, "登录成功", token)
}

// 设备绑定
func EquipmentBinding(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.EquipmentBinding
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	_, err := inits.UserCline.EquipmentBinding(c, &h.EquipmentBindingReq{
		UserId:      int64(userId),
		EquipmentId: req.EquipmentId,
	})
	if err != nil {
		response.Error(c, 400, "绑定失败", err.Error())
		return
	}
	response.Success(c, "绑定成功", nil)
}

// Del
// @Summary      用户注销
// @Description  用户注销
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.Del  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /user/del [post]
func Del(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.Del
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	_, err := inits.UserClient.UserDel(context.Background(), &__.UserDelReq{
		Id: int32(userId),
	})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "用户注销成功", nil)
}

// UserUpdatePassword
// @Summary      用户修改密码
// @Description  用户修改密码
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.UserUpdatePassword  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /user/userUpdatePassword [post]
func UserUpdatePassword(c *gin.Context) {
	userId := c.GetUint("userId")
	fmt.Println("userId", userId)
	var req request.UserUpdatePassword
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	user, err := inits.UserClient.UserUpdatePassword(context.Background(), &__.UserUpdatePasswordReq{
		Id:          int32(userId),
		Password:    req.Password,
		NewPassword: req.NewPassword,
		RePassword:  req.RePassword,
	})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}

	response.Success(c, "用户修改密码成功", user.Id)
}

// UserProfileCreated
// @Summary      用户添加个人资料
// @Description  用户添加个人资料
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.UserProfileCreated  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /user/userProfileCreated [post]
func UserProfileCreated(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.UserProfileCreated
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	userProfile, err := inits.UserClient.UserProfileCreated(context.Background(), &__.UserProfileCreatedReq{
		UserId:    uint64(userId),
		NickName:  req.NickName,
		Gender:    req.Gender,
		Email:     req.Email,
		Signature: req.Signature,
	})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "用户添加个人资料成功", userProfile.Id)
}

// UserProfileUpdated
// @Summary      用户修改个人资料
// @Description  用户修改个人资料
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.UserProfileUpdated  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /user/userProfileUpdated [post]
func UserProfileUpdated(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.UserProfileUpdated
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	_, err := inits.UserClient.UserProfileUpdated(context.Background(), &__.UserProfileUpdatedReq{
		UserId:    uint64(userId),
		NickName:  req.NickName,
		Gender:    req.Gender,
		Email:     req.Email,
		Signature: req.Signature,
	})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "用户修改个人资料成功", userId)
}

// UserProfileDetail
// @Summary      用户个人资料详情
// @Description  用户个人资料详情
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        user  query      request.UserProfileUpdated  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /user/userProfileDetail [get]
func UserProfileDetail(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.UserProfileUpdated
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	detail, err := inits.UserClient.UserProfileDetail(context.Background(), &__.UserProfileDetailReq{UserId: uint64(userId)})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "用户个人资料详情展示成功", detail)
}

// UserProfileCreated
// @Summary      用户意见反馈
// @Description  用户意见反馈
// @Tags         用户模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.UserOpinionAdd  true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Security TokenAuth
// @Router       /user/userOpinionAdd [post]
func UserOpinionAdd(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.UserOpinionAdd
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}

	add, err := inits.UserClient.UserOpinionAdd(context.Background(), &__.UserOpinionAddReq{
		UserId:         uint64(userId),
		Name:           req.Name,
		Mobile:         req.Mobile,
		OpinionType:    req.OpinionType,
		OpinionTitle:   req.OpinionTitle,
		OpinionDetails: req.OpinionDetails,
	})
	if err != nil {
		response.Error(c, 400, err.Error(), nil)
		return
	}
	response.Success(c, "用户意见反馈成功", add.Id)
}

// 人脸注册
func RegisterFace(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.RegisterFaceReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	face, err := inits.UserClient.RegisterFace(c, &__.RegisterFaceReq{
		Userid:  int64(userId),
		FaceImg: req.FaceImg,
	})
	if err != nil {
		response.Error(c, 400, "人脸注册失败", err.Error())
		return
	}
	response.Success(c, "人脸注册成功", face.Faceid)
}

// 人脸登录
func LoginFace(c *gin.Context) {
	var req request.LoginFaceReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	face, err := inits.UserClient.LoginFace(c, &__.LoginFaceReq{FaceImg: req.FaceImg})
	if err != nil {
		response.Error(c, 400, "人脸登录失败", err.Error())
		return
	}
	response.Success(c, "人脸登录成功", face.Userid)
}

// 收藏添加
func AddCollect(c *gin.Context) {
	var req request.AddCollectReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	collect, err := inits.UserClient.AddCollect(c, &__.AddCollectReq{
		UserId:  req.UserId,
		GoodsId: req.GoodsId,
	})
	if err != nil {
		response.Error(c, 400, "收藏添加失败", err.Error())
		return
	}
	response.Success(c, "收藏添加成功", collect.CollectId)
}

// 收藏展示
func ListCollect(c *gin.Context) {
	var req request.CollectInfoReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	info, err := inits.UserClient.CollectInfo(c, &__.CollectInfoReq{})
	if err != nil {
		response.Error(c, 400, "收藏展示失败", err.Error())
		return
	}
	response.Success(c, "收藏展示成功", info.List)
}

// 收藏删除
func DeleteCollect(c *gin.Context) {
	var req request.DeleteCollectReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, 400, "参数错误", err.Error())
		return
	}
	collect, err := inits.UserClient.DeleteCollect(c, &__.DeleteCollectReq{Id: int64(req.Id)})
	if err != nil {
		response.Error(c, 400, "收藏删除失败", err.Error())
		return
	}
	response.Success(c, "收藏删除成功", collect.Id)
}
